import TileLayer from "ol/layer/Tile"
import TileArcGISRest from 'ol/source/TileArcGISRest'
import OSM from "ol/source/OSM"
import XYZ from 'ol/source/XYZ'

let maptype = 0;          //0表示部署的离线瓦片地图，1表示OSM, 2表示使用Arcgis在线午夜蓝地图服务

var streetmap = function () {
    var maplayer = null;
    var _url = "http://localhost:8080/";
    switch (maptype) {
        case 0:
            maplayer = new TileLayer({
                source: new XYZ({
                    tileUrlFunction: function (xyz) {
                        var z = xyz[0];
                        var x = Math.abs(xyz[1]);
                        var y = Math.abs(xyz[2]) - 2;
                        x = 'C' + padLeft(8, x.toString(16));
                        y = 'R' + padLeft(8, y.toString(16));
                        z = 'L' + padLeft(2, z);
                        var url = _url + z + '/' + y + '/' + x + '.png';
                        console.log(x, y, z, url);
                        return url;
                    }

                    // tileUrlFunction: function (coordinate) {
                    //     //alert(coordinate[0] + " X= " + coordinate[1] + " Y= " + coordinate[2]);
                    //     var x = 'C' + zeroFill(coordinate[1], 8, 16);
                    //     var y = 'R' + zeroFill(coordinate[2] - 1, 8, 16);
                    //     var z = 'L' + zeroFill(coordinate[0], 2, 10);
                    //     var url = 'http://localhost:8080/' + z + '/' + y + '/' + x + '.png';
                    //     console.log(x, y, z, url);
                    //     return url;
                    // }
                })
            })
            break;
        case 1:
            maplayer = new TileLayer({
                source: new OSM()
            })
            break;
        case 2:
            maplayer = new TileLayer({
                source: new TileArcGISRest({
                    url: 'https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer'
                })
            })
            break;
    }
    return [maplayer];
}

//将10进制转16进制，余位补0，凑成ArcServer的切片格式
function padLeft(num, val) {
    return (new Array(num).join('0') + val).slice(-num);
}

// function zeroFill(num, len, radix) {
//     var str = num.toString(radix || 10);
//     while (str.length < len) {
//         str = "0" + str;
//     }
//     return str;
// }

var mapconfig = {
    x: 114.064839,     //中心点经度和纬度
    y: 22.548857,
    zoom: 5,          //地图缩放级别
    streetmap: streetmap
};

export default mapconfig
